package com.littcore.saap.assistant.web;



import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.littcore.common.Utility;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.exception.NotLoginException;
import com.littcore.saap.assistant.dao.UserFavouriteDao;
import com.littcore.saap.assistant.po.UserFavourite;
import com.littcore.saap.assistant.service.IUserFavouriteService;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.web.util.WebUtils;

@Controller
public class UserFavouriteController {

	@Resource
	private IUserFavouriteService userFavouriteService;
	
	@Resource
	private UserFavouriteDao favouriteDao;
	
	  @RequestMapping 
	  public ModelAndView getUserFavourites(WebRequest request) throws NotLoginException
	  { 
	    String moduleCode = request.getParameter("moduleCode");
	    
	    //package the params
	    PageParam pageParam = WebUtils.getPageParam(request);
	    pageParam.addCond("tenantId", LoginUtils.getTenantId());
	    pageParam.addCond("moduleCode", moduleCode);  
	    pageParam.addCond("createBy", LoginUtils.getLoginOpId());  
	    pageParam.addCond("isDeleted", false);  
	    pageParam.addSort("createDatetime", "desc");
	    //Get page result
	    IPageList pageList =userFavouriteService.listPage(pageParam);   
	    
	    return new ModelAndView("jsonView").addObject("userFavourites", pageList.getRsList()); 
	  
	  } 
	  
	  @RequestMapping 
	  public ModelAndView add(WebRequest request)
	  { 
	    String moduleCode = request.getParameter("moduleCode");
	    Integer recordId = Utility.parseInt(request.getParameter("recordId"), null);
	    
	    UserFavourite favourite=new UserFavourite();
	    favourite.setCreateBy(LoginUtils.getLoginOpId().intValue());
	    favourite.setCreateDatetime(new Date());
	    favourite.setModuleCode(moduleCode);
	    favourite.setRecordId(recordId);
	    favourite.setTenantId(LoginUtils.getTenantId());
	    favourite.setParams("");
	    
	    if(favouriteDao.findUnique(favourite.getModuleCode(), favourite.getRecordId(), favourite.getCreateBy(), favourite.getTenantId())==null){
	    	 favouriteDao.save(favourite);
	    }
		    
	    return null; 
	  
	  } 
	  
	  @RequestMapping 
	  public void delete(@RequestParam Integer id)
	  { 
		 favouriteDao.deleteById(id);
	  }
	  

}
